package org.seedltd.upms.service.impl.sys;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.base.BaseServiceImpl;
import org.seedltd.core.utils.Assert;
import org.seedltd.core.utils.BeanUtils;
import org.seedltd.upms.enums.EnableStatus;
import org.seedltd.upms.mapper.sys.SysMenuMapper;
import org.seedltd.upms.mapper.sys.SysRoleMapper;
import org.seedltd.upms.model.sys.entity.SysRole;
import org.seedltd.upms.model.sys.form.SysRoleForm;
import org.seedltd.upms.model.sys.form.SysRoleSearchForm;
import org.seedltd.upms.model.sys.vo.SysRoleVO;
import org.seedltd.upms.service.sys.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.AllArgsConstructor;

/**
 * 角色管理业务实现
 *
 * @author laizuan
 * @since 2021/07/19
 */
@Service
@AllArgsConstructor
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    private final SysMenuMapper sysMenuMapper;

    @Override
    public SysRoleVO findById(Long id) {
        return q().selectById(id, SysRoleVO.class);
    }

    @Override
    public SysRole findByRoleCode(String roleCode) {
        return q().eq(SysRole::getRoleCode, roleCode).eq(SysRole::getEnabled, EnableStatus.ENABLE.getValue())
            .selectOne();
    }

    @Override
    public Set<Long> listMenuIdByRoleId(Long roleId) {
        return sysMenuMapper.listMenuIdByRoleId(roleId);
    }

    @Override
    @Transactional
    public Long add(SysRoleForm form) {
        Assert.isNull(baseMapper.checkCodeExists(form.getRoleCode()),
            String.format("角色代码【%s】已存在，请重新输入。", form.getRoleCode()));
        SysRole pojo = new SysRole();
        BeanUtils.copyProperties(form, pojo);
        baseMapper.insert(pojo);
        sysMenuMapper.insertMenuRole(pojo.getRoleId(), form.getMenuIdList());
        return pojo.getRoleId();
    }

    @Override
    @Transactional
    public boolean update(SysRoleForm form) {
        SysRole pojo = baseMapper.selectById(form.getRoleId());
        Assert.notNull(pojo, "未找到需要修改的数据");
        BeanUtils.copyProperties(form, pojo, "roleCode");
        sysMenuMapper.deleteMenuRoleByRoleId(form.getRoleId());
        if (CollectionUtils.isNotEmpty(form.getMenuIdList())) {
            sysMenuMapper.insertMenuRole(form.getRoleId(), form.getMenuIdList());
        }
        return baseMapper.updateById(pojo) > 0;
    }

    @Override
    @Transactional
    public boolean delete(Long id) {
        sysMenuMapper.deleteMenuRoleByRoleId(id);
        sysMenuMapper.deleteUserRoleByRoleId(id);
        sysMenuMapper.deleteDeptRoleByRoleId(id);
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public BasePage<SysRoleVO> page(SysRoleSearchForm searchForm) {
        return q(searchForm).orderByDesc(SysRole::getCreateTime)
            .selectPage(BasePage.of(searchForm.getStart(), searchForm.getSize()), SysRoleVO.class);
    }

    @Override
    public List<SysRoleVO> listValid() {
        return q().select(SysRole::getRoleId, SysRole::getRoleName, SysRole::getRemark)
            .eq(SysRole::getEnabled, EnableStatus.ENABLE.getValue()).selectList(SysRoleVO.class);
    }
}